<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Recruit as RecruitModel;

/**
 * 企业招聘
 * Class RecruitService
 * @package app\admin\service
 */
class RecruitService
{
    /**
     * 企业招聘列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecruitList($where, $page, $pagesize)
    {
        $data = RecruitModel::getRecruitList($where, $page, $pagesize);

        foreach ($data as $k => $v) {
            $data[$k]['headimg'] = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div><p>' . emojiDecode($v['nickname']) . '</p>';

            // 薪资范围
            $data[$k]['salary'] = ($v['min_salary'] == $v['max_salary'] ? $v['min_salary'] : $v['min_salary'] . '-' . $v['max_salary']) . 'K';

            // 已驳回，获取最新原因
            $data[$k]['remarks'] = '-';
            if ($v['status'] == -1) {
                $data[$k]['remarks'] = RecruitRejectService::findRecruitRejectValue(['recid' => $v['id']]);
            }

            // 行业
            $data[$k]['cname'] = '<span class="mySpan">' . IndustryCateService::findIndustryCateValue(['id' => $v['cid']]) . '</span>';

            // 二维码
            $data[$k]['qrcode'] = '<div><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['qrcode'] . '" \' + \'alt=""></div>';
        }

        return $data;
    }

    /**
     * 企业招聘统计
     * @param $where
     * @return int
     */
    public static function countRecruitList($where)
    {
        return RecruitModel::countRecruitList($where);
    }

    /**
     * 指定企业招聘
     * @param $where
     * @param int $assemble 0不组装  1组装
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findRecruit($where, $assemble = 0)
    {
        $info = RecruitModel::findRecruit($where);
        if (!$assemble) {
            return $info;
        }

        // 薪资范围
        $info['salary'] = ($info['min_salary'] == $info['max_salary'] ? $info['min_salary'] : $info['min_salary'] . '-' . $info['max_salary']) . 'K';
        // 工作经验
        $info['wname'] = WorkExperienceService::findWorkExperienceValue(['id' => $info['weid']]);
        // 学历要求
        $info['rname'] = EducationRequirementService::findEducationRequirementValue(['id' => $info['reqid']]);

        // 企业信息
        $enterprise = EnterpriseService::findEnterprise(['id' => $info['eid']], 1);
        // 名称
        $info['name'] = $enterprise['name'];
        // 职位
        $info['belong_position'] = $enterprise['belong_position'];
        // 电话
        $info['mobile'] = $enterprise['mobile'];
        // 二维码
        $info['qrcode'] = $enterprise['qrcode'];
        // logo
        $info['logo'] = $enterprise['logo'];
        // 介绍
        $info['introduce'] = $enterprise['introduce'];
        // 省份
        $info['province'] = $enterprise['province'];
        // 城市
        $info['city'] = $enterprise['city'];
        // 地区
        $info['area'] = $enterprise['area'];
        // 昵称
        $info['nickname'] = $enterprise['nickname'];
        // 头像
        $info['headimg'] = $enterprise['headimg'];
        // 行业
        $info['cname'] = $enterprise['cname'];
        // 企业图片
        $info['paths'] = $enterprise['paths'];

        return $info;
    }

    /**
     * 更新企业招聘
     * @param $data
     * @param $where
     * @return RecruitModel
     */
    public static function updateRecruit($data, $where)
    {
        return RecruitModel::updateRecruit($data, $where);
    }

    /**
     * 创建企业招聘
     * @param $data
     * @return RecruitModel|\think\Model
     */
    public static function createRecruit($data)
    {
        return RecruitModel::createRecruit($data);
    }
}